package org.the.gsdo;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.SimpleThreadScope;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;

// junit :: we have to fake the scopes
public class UnitWebContextExecutionListener extends AbstractTestExecutionListener {
	public UnitWebContextExecutionListener() {}
    @Override
    public void prepareTestInstance(TestContext testContext) throws Exception {
        if (testContext.getApplicationContext() instanceof GenericApplicationContext) {
            GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext();
            ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
            org.springframework.beans.factory.config.Scope requestScope = new SimpleThreadScope();
            beanFactory.registerScope("request", requestScope);
            org.springframework.beans.factory.config.Scope sessionScope = new SimpleThreadScope();
            beanFactory.registerScope("session", sessionScope);
        }
    }
}
